﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;



using OF.Component.Model;
using OF.Utility;
using OF.Utility.Logging;
using System.Text;
using System.Web;
using System.Xml.XPath;
using CWQ.Api.Common;
using CWQ.Api.Models;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OF.Component.Base;
using OF.Component.BLL;
using OF.Component.DTO;
using Swashbuckle.Swagger.Annotations;



namespace CWQ.Api.Controllers
{
    /// <summary>
    /// 通用控制器
    /// </summary>
    public class CommonController : BaseApiController
    {
        /// <summary>
        /// 文件上传接口
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Upload()
        {
            var needWaterMark = true;
            var response=new ResponseUpload();
            var result ="";
            var dir = AppSettings.UploadFileDir;
            var szHttpFileFullPath = AppSettings.UploadFileFullHttpDir;
            var szHttpFileRelativePath = AppSettings.UploadFileRelativeHttpDir;
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var oFile = HttpContext.Current.Request.Files[0];
            string fileName = Guid.NewGuid() + ".jpg";
            Image originalImg = Image.FromStream(oFile.InputStream);
            //打水印
            if (needWaterMark)
            {
                originalImg = WaterMarkImage(originalImg);
            }
            var szFilePath = Path.Combine(dir, fileName);
            //oFile.SaveAs(szFilePath);
            originalImg.Save(szFilePath);
            //给图片加水印
            
            var imgFullPath =szHttpFileFullPath+fileName;
            var imgRelativePath = szHttpFileRelativePath+fileName;
            result = new ResponseUpload()
            {
                FileHttpFullPath = imgFullPath,
                FileRelativeFullPath = imgRelativePath
            }.ToJSON();
            return result;
        }

        /// <summary>
        /// 给图片添加水印
        /// </summary>
        /// <param name="img"></param>
        Image WaterMarkImage(Image img)
        {
            
            string text = "车务圈";
            ImageHelper.WatermarkPosition position = ImageHelper.WatermarkPosition.BottomRight;
            var font = new Font("Verdana", 12, FontStyle.Italic);
            var brush = new SolidBrush(Color.White);
            return ImageHelper.WatermarkText(img, text, position, font, brush);
            //img.Save(imgFilePath);
        }

        /// <summary>
        /// 发送验证
        /// </summary>
        /// <param name="mobile">电话号码</param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<string> SendVerifyCode(string mobile)
        {
            ResultData<string> rData = new ResultData<string>();
            string rtnMsg = "";

            bool result = SendMessage.SendCheckCodePost(mobile, AppSettings.ShortMessagePlatformId, "店铺注册验证码", 1, 0, "", AppSettings.ShortMessageUrl, out rtnMsg);

            if (!result)
            {
                rData.status = -1;
                rData.message = rtnMsg;

                //添加错误日志
                LogClient.Current.Error(new ErrorLog()
                {
                    ErrorMessage = rtnMsg,
                    PlatformID = 1,
                    RequestUrl = "Common/SendVerifyCode",
                    PostData = mobile
                });
            }
            return rData;
        }

        /// <summary>
        /// 获取业务分组
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public ResultData<IList<BizTypeData>> GetBizTypeInfoList()
        {
            ResultData<IList<BizTypeData>> rData = new ResultData<IList<BizTypeData>>();
            IList<BizTypeGroupInfo> list = BizTypeGroupOperate.QueryList(m=>m.GroupID>0);
            BizTypeData info = null;
            IList<BizTypeData> btList = new List<BizTypeData>();
            foreach (var item in list)
            {
                IList<BizTypeInfo> bizList = BizTypeOperate.QueryList(m => m.GroupID == item.GroupID);
                info = new BizTypeData
                {
                    GroupID = item.GroupID,
                    GroupName = item.GroupName,
                    Data = bizList
                };
                btList.Add(info);
            }
            rData.data = btList;
            rData.status = 0;
            return rData;
        }

    }
}
